/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.results.conclusion;

import java.awt.Frame;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import systole.domain.report.Report;
import systole.domain.report.template.Conclusion;
import systole.exceptions.ExceptionDAO;
import systole.view.crud.conclusion.ControllerConclusionEdition;
import systole.view.crud.conclusion.ControllerConclusionView;
import systole.view.tabs.results.ResultDialogController;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class ControllerConclusion extends ResultDialogController {

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
        dialogConclusion.getjCmbConclusions().removeAllItems();

        List<Conclusion> conclusions = ("F".equals(this.patient.getSex()))
                ? facadeDB.getConclusionBroker().getConclusionsForFemale()
                : facadeDB.getConclusionBroker().getConclusionsForMale();
        Iterator<Conclusion> it = conclusions.iterator();
        dialogConclusion.getjCmbConclusions().removeAllItems();
        while (it.hasNext()) {
            dialogConclusion.getjCmbConclusions().addItem(it.next());
        }

        AutoCompleteDecorator.decorate(dialogConclusion.getjCmbConclusions());
    }

    @Override
    protected JDialog createForm(Frame parent) {
        return new JDialogConclusion(parent, this);
    }

    @Override
    protected void loadIconOnForm() {
    }

    @Override
    protected void loadReportOnForm(Report report) {
        JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
        dialogConclusion.getjTxtConclusion().setText(report.getConclusion() != null ? report.getConclusion() : "");
    }

    @Override
    protected void loadReportFromForm() {
        JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
        this.reportModel.updateConclusion(dialogConclusion.getjTxtConclusion().getText());
    }

    public void add() {
        JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
        if ((dialogConclusion.getjCmbConclusions().getSelectedItem() != null) && (dialogConclusion.getjCmbConclusions().getSelectedIndex() >= 0)) {
            Conclusion conclusion = (Conclusion) dialogConclusion.getjCmbConclusions().getSelectedItem();
            dialogConclusion.getjTxtConclusion().append(conclusion.getText());
        }
    }

    public void newConclusion() {
        JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
        ControllerConclusionEdition controllerConclusionEdition = new ControllerConclusionEdition();
        controllerConclusionEdition.newEntity();
        try {
            List<Conclusion> conclusions = facadeDB.getConclusionBroker().getAllConclusions();
            Iterator<Conclusion> it = conclusions.iterator();
            dialogConclusion.getjCmbConclusions().removeAllItems();
            while (it.hasNext()) {
                dialogConclusion.getjCmbConclusions().addItem(it.next());
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            ErrorDialog.showError(this.form, ex.getMessage());
        }
    }

    public void search() {
        try {
            JDialogConclusion dialogConclusion = (JDialogConclusion) this.form;
            ControllerConclusionView controllerConclusionView = new ControllerConclusionView();
            controllerConclusionView.showFormToSelect(null);
            if (controllerConclusionView.getSelectedEntity() != null) {
                dialogConclusion.getjCmbConclusions().setSelectedItem(controllerConclusionView.getSelectedEntity());
            }
        } catch (ExceptionDAO ex) {
            this.facadeDB.refreshSession();
            ErrorDialog.showError(this.form, ex.getMessage());
        }
    }
}
